package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.event.util.EventUtil;
import com.qf.annotation.CacheUsing;
import com.qf.cache.EhcacheUtil;
import com.qf.dao.HotalDao;
import com.qf.entity.*;
import com.qf.feign.CityFeign;
import com.qf.service.HotalService;
import com.qf.service.RoomPriceService;
import com.qf.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * (Hotal)表服务实现类
 *
 * @author makejava
 * @since 2020-09-19 19:57:01
 */
@Service("hotalService")
public class HotalServiceImpl extends ServiceImpl<HotalDao, Hotal> implements HotalService {
    @Autowired
    private RoomService roomService;
    @Autowired
    private RoomPriceService roomPriceService;
    @Autowired
    private EventUtil eventUtil;
    @Autowired
    private CityFeign cityFeign;
/*
    private RabbitTemplate rabbitTemplate;
*/
    @Override
    public List<Hotal> list() {
        List<Hotal> hotals = super.list();
        hotals.stream().forEach(hotal -> {
            List<Room> rooms = roomService.list(new QueryWrapper<Room>().eq("hid", hotal.getId()));

            rooms.stream().forEach(room -> {
                List<RoomPrice> roomPrices = roomPriceService.list(new QueryWrapper<RoomPrice>().eq("rid", room.getId()));

                room.setPrices(roomPrices);
            });

            hotal.setRooms(rooms);
        });
        return hotals;
    }

    @Override
    public boolean save(Hotal entity) {
        boolean flag = super.save(entity);

        /*searchFeign.syncHotal(entity);*/
        eventUtil.publishEvent("hotal_insert",entity);

/*
        rabbitTemplate.convertAndSend("hotal_exchange","insert",entity);
*/
        return flag;
    }

    @Override
    @CacheUsing(key = "'hotal_' + #id")
    public Hotal getById(Serializable id) {
        System.out.println("查询酒店详情信息--------");
        Hotal hotal = super.getById(id);

        R<City> cityR = cityFeign.queryCityById(hotal.getCid());
        hotal.setCity(cityR.getData());

        List<Room> rooms = roomService.list(new QueryWrapper<Room>().eq("hid",hotal.getId()));
        hotal.setRooms(rooms);

        rooms.stream().forEach(room -> {
            List<RoomPrice> priceList = roomPriceService.list(new QueryWrapper<RoomPrice>().eq("rid",room.getId()));
            room.setPrices(priceList);
        });
        return hotal;
    }
}